<?php
/**
 * User: Jerry
 * Date: 2017/11/10
 * Time: 14:47
 */

namespace SDXApi\Model;


use Think\Model;

/**
 * Class HomeTemplateGoodsNewModel
 * @author Jerry
 * @package SDXApi\Model
 */
class HomeTemplateGoodsNewModel extends Model
{
    /**
     * @var string
     */
    protected $tableName = 'goods';

    /**
     * @author Jerry
     * @param int $wid
     * @param int $limit
     * @param int $pageNumber
     * @return mixed
     */
    public function findGoodsNew($wid = 8, $limit = 100, $pageNumber = 1)
    {

        $pageArr = show_page($limit, $pageNumber);

        $where = array(
            'wg.is_delete'	    =>	0,
            'wg.is_on_sale'	    =>	1,
            'g.is_alone_sale'   =>  1,
            'g.original_img'    =>  array('NEQ', ''),
            'g.goods_img'       =>  array('NEQ', ''),
            'gs.practical_num'  =>  array('GT', 0),
            'lg.i'              =>  array('exp', 'is null'),
//            'g.goods_id'        =>  array('not in', 'SELECT link_goods_id from sdx_link_goods'),
        );

        $field = 'g.goods_id,g.goods_sn,g.goods_name,g.goods_thumb,g.goods_img,g.goods_brief,g.is_presell,g.promote_price,g.promote_start_date,g.promote_end_date,g.goods_number,g.warn_number,g.original_img,g.is_on_sale,g.promote_goods_number,g.promote_today_number,g.is_buynow,g.spec_unit,g.spec_value,g.min_buy_num,g.delivery_type,g.delivery_cycle,gs.practical_num,g.promote_user_rank,g.promote_image';

        $join = "LEFT JOIN __GOODS_STORAGE__ AS gs ON g.goods_id = gs.goods_id AND gs.wid = $wid";
        $wJoin = "LEFT JOIN sdx_goods_wid_$wid AS wg ON g.goods_id = wg.goods_id";
        $linkJoin = "LEFT JOIN (SELECT link_goods_id as i FROM sdx_link_goods) as lg ON g.goods_id = lg.i";

        return $this->where($where)
                    ->alias('g')
                    ->field($field)
                    ->join($join)
                    ->join($wJoin)
                    ->join($linkJoin)
                    ->where($where)
                    ->order('g.add_time desc')
                    ->limit($pageArr['firstpage'] . ',' . C('page_size'))
                    ->group('g.goods_id')
                    ->select();
    }


}